﻿using System;
using System.Reflection;
using System.Diagnostics;

namespace Common.Utils.Helper
{
    public class ReflectionHelper
    {
        /// <summary>
        /// 创建对象实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="nameSpace">命名空间</param>
        /// <param name="className">类型名</param>
        /// <returns></returns>
        public static T CreateInstance<T>(string nameSpace, string className, params Object[] args)
        {
            return CreateInstance<T>(nameSpace, className, Assembly.GetExecutingAssembly().FullName, args);
        }


        /// <summary>
        /// 创建对象实例
        /// </summary>
        /// <typeparam name="T">要创建对象的类型</typeparam>
        /// <param name="assemblyName">类型所在程序集名称</param>
        /// <param name="nameSpace">类型所在命名空间</param>
        /// <param name="className">类型名</param>
        /// <returns></returns>
        public static T CreateInstance<T>(string nameSpace, string className, string assemblyName, params Object[] args)
        {
            try
            {
                string fullName = nameSpace + "." + className;      //命名空间.类型名
                string path = fullName + "," + assemblyName;        //命名空间.类型名,程序集
                Type o = Type.GetType(path);                        //加载类型
                object obj;
                if (null == args || 0 == args.Length)
                {
                    obj = Activator.CreateInstance(o, true);        //根据类型创建实例
                }
                else
                {
                    obj = Activator.CreateInstance(o, args);        //根据类型创建实例
                }
                return (T)obj;                                      //类型转换并返回
            }
            catch
            {
                //发生异常，返回类型的默认值
                return default(T);
            }
        }


        /// <summary>
        /// 获取对象的属性值
        /// </summary>
        /// <typeparam name="T">查询的字段类型</typeparam>
        /// <typeparam name="O">查询对象的类型</typeparam>
        /// <param name="_obj">查询的对象</param>
        /// <param name="_fieldName">查询的字段名称</param>
        /// <returns></returns>
        public static T GetObjectValue<T, O>(O _obj, string _field)
        {
            try
            {
                Type tObj = _obj.GetType();
                object obj = tObj.GetProperty(_field).GetValue(_obj, null);
                return (T)obj;
            }
            catch
            {
                //发生异常，返回类型的默认值
                return default(T);
            }
        }


        /// <summary>
        /// 获取当前方法所在类名
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentClassName()
        {
            try
            {
                StackTrace st = new StackTrace(true);
                MethodBase mb = st.GetFrame(1).GetMethod();

                if (null == mb.DeclaringType.DeclaringType)
                    return mb.DeclaringType.Name;
                return mb.DeclaringType.DeclaringType.Name;
            }
            catch
            {
                //发生异常，返回未知名称
                return "UnknowClassName";
            }
        }
    }
}
